// import $store from '@/store'
import uni_request from '@/utils/uni_request/uni_request.js'
import api from '../api/config.js'

let baseURL

if (process.env.NODE_ENV === 'development') {
	baseURL = api.baseURL
} else {
	baseURL = api.baseURL
}

const request = uni_request({
	baseURL
})

request.interceptors.request.use(async (config, ...args) => { // 请求拦截器
	// ...
	// if (!$store.state.app.token) { // 如果没得 token
	// 	// await new Promise(resolve => ))
	// }
	var timestamp = new Date().getTime()
	config.body.timestamp = timestamp
	config.body.xcxflag = api.xcxflag
	config.body.belong = api.belong
	config.body.openid = uni.getStorageSync('openid')
	config.body.onlycode = uni.getStorageSync('onlycode')
	config.body.dian_token = uni.getStorageSync('DIAN_TOKEN')


	console.log(config, "请求参数")
	// config.header.Authorization = 'Bearer ' + $store.state.app.token // 把 token 放在请求头
	return config
})

request.interceptors.response.use(async (response, ...args) => { // 响应拦截器
	// ...
	console.log(response, "响应结果")
	return response
})

request.onerror = (...args) => console.log(args) // 错误监听
var $request = request
export default $request
